条件编译
Rust 有一个特殊的属性,,它允许你基于一个传递给编译器的标记编译代码。它有两种形式:
它还有一些帮助选项:
#[cfg(any(unix, windows))]
# fn foo() {}
#[cfg(all(unix, target_pointer_width = "32"))]
# fn bar() {}
#[cfg(not(foo))]
# fn not_foo() {}
这些选项可以任意嵌套:
[features]
# no features by default
# Add feature "foo" here, then you can use it.
foo = []
当你这么做的时候,Cargo传递给rustc
一个标记:
这些cfg
标记集合会决定哪些功能被启用,并且因此,哪些代码会被编译。让我们看看这些代码:
#[cfg(feature = "foo")]
mod foo {
}
如果你用cargo build --features "foo"
编译,他会向rustc
传递--cfg feature="foo"
标记,并且输出中将会包含。如果我们使用常规的cargo build
编译,则不会传递额外的标记,因此,(输出)不会存在foo
模块。
如果a
通过cfg
属性设置了的话这与#[b]
相同,否则不起作用。
cfg!
cfg!
语法扩展也让你可以在你的代码中使用这类标记:
if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
println!("Think Different!");
}
这会在编译时被替换为一个true
或,依配置设定而定。